package com.jimikongjian.service.modal;

import com.jimikongjian.dao.IArticle;
import com.jimikongjian.dao.IUpvote;
import com.jimikongjian.models.Article;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.Upvote;
import com.jimikongjian.service.ArticleService;
import com.jimikongjian.service.UserService;
import com.jimikongjian.service.base.IUpvoteService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by fuxiuyang on 17-5-14.
 */
@Service("upvoteService")
public class UpvoteService implements IUpvoteService {

    @Autowired
    @Qualifier("iUpvoteImpl")
    private IUpvote iUpvoteImpl;

    @Autowired
    @Qualifier("iArticleImpl")
    private IArticle iArticleImpl;

    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("articleService")
    private ArticleService articleService;

    /**
     * 对某一篇文章点赞
     *
     * @param upvote
     */
    @Override
    @Transactional
    public ResponseMessage upvoteArticle(Upvote upvote) {
        if (upvote.getArticleId() > 0 && upvote.getUserId() > 0){
            if (userService.hasUser(upvote.getUserId())){
                if (articleService.hasArticle(upvote.getArticleId())){
                    if (!iUpvoteImpl.isUpvote(upvote.getUserId(), upvote.getArticleId())){
                        iUpvoteImpl.insertUpvote(upvote);
                        articleService.updateAddArticleUpvoteCount(upvote.getArticleId());
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.SYSTEM_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 判断该用户是否已经点过赞
     *
     * @param upvote
     * @return
     */
    @Override
    public boolean hasUpvote(Upvote upvote) {
        return false;
    }

    /**
     * 对某一篇文章取消点赞
     *
     * @param upvote
     */
    @Override
    @Transactional
    public ResponseMessage deleteUpvote(Upvote upvote) {
        if (upvote.getArticleId() > 0 && upvote.getUserId() > 0){
            if (userService.hasUser(upvote.getUserId())){
                if (articleService.hasArticle(upvote.getArticleId())){
                    if (iUpvoteImpl.isUpvote(upvote.getUserId(), upvote.getArticleId())){
                        iUpvoteImpl.deleteByUserIdAndArticleId(upvote);
                        articleService.updateSubArticleUpvoteCount(upvote.getArticleId());
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.SYSTEM_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
